<template>
  <div class="app-container">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl" style="z-index: 999">
          <ProSelect v-model="filter.pro_id" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <el-select
            v-model="filter.sup_id"
            size="small"
            :remote-method="selSups"
            remote
            filterable
            style="width: 100%"
            placeholder="请选供应商"
            clearable
            @focus="selSups"
          >
            <el-option
              v-for="item in sels"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="getList">查 询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table ref="multipleTable" class="search-table" :data="list" border 
      :summary-method="getSummaries"
      show-summary
      fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="公司名称" width="160px" prop="company" />
      <el-table-column label="项目名称" width="220px" prop="pro_name" />
      <el-table-column label="PO单号" width="190px">
        <template slot-scope="{row}">
          {{ 'PO'+row.pro_id }}
        </template>
      </el-table-column>
      <el-table-column label="供应商" width="220px" prop="sup_name" />
      <el-table-column label="合同编号" width="190px">
        <template slot-scope="{row}">
          {{ 'LOPO'+row.sn }}
        </template>
      </el-table-column>
      <el-table-column label="发包内容" width="220px" prop="name" />
      <!-- <el-table-column label="采购金额" width="110px" align="right" prop="price" /> -->
      <el-table-column label="状态" width="100px" align="center" :formatter="out" />
      <el-table-column label="履行地点" width="260px" prop="addr" />
      <el-table-column label="签订日期" width="110px" align="center" prop="pur_time" />
      <el-table-column label="品名" width="220px" prop="name" />
      <el-table-column label="合同金额" width="110px" align="right">
        <template slot-scope="{row}">
          {{ app.toFix(row.price, 2) }}
        </template>
      </el-table-column>
      <el-table-column label="累计付款" width="110px" align="right" :formatter="ljfk" />
      <el-table-column label="余额" width="110px" align="right" :formatter="ye" />
      <el-table-column label="申请人" width="120px" prop="nickname" />
      <el-table-column label="申请时间" align="center" width="140px" prop="apply_time" />
      <el-table-column label="备注" min-width="260px" prop="feedback" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="files({e_id: row.id, type: '',
                                      db_type: 'logc_purchase_outsource_archives'})"
              >
                附 件
              </el-dropdown-item>
              <el-dropdown-item
                @click.native="handleEditBefore(row)"
              >
                外 包
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Cos ref="cos" :info="dialog" />
  </div>
</template>

<script>
import { selSups } from '@/api/supplier'
import { puros, out } from '@/api/purchase'
import ProSelect from '@/components/ProSelect'
import page from '@/utils/page'

export default {
  name: '',
  components: { ProSelect },
  extends: page,
  props: {
    reqList: {
      default: puros,
      type: Function
    },
    reqUpd: {
      default: out,
      type: Function
    }
  },
  data() {
    return {
      eo_price: [0, 0, 0],
      sels: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    selSups(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selSups({ name: str }).then(response => {
        this.sels = response.list
      }).catch(err => {
        this.$message.error(err)
      })
    },
    handleEditBefore(e) {
      this.$confirm('此操作将修改数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.toEdit(e).then(async() => {
          this.getList()
        })
      })
    },
    out(e) {
      let s = '未外包'
      if (e.state === 1) s = '已外包'
      return s
    },
    ljfk(e) {
      return this.app.toFix(e.payment - e.deduction, 2)
    },
    ye(e) {
      return this.app.toFix(e.price - (e.payment - e.deduction), 2)
    },
    getListAfter(e) {
      this.eo_price[0] = e.summary.price
      this.eo_price[1] = e.summary.payment
      this.eo_price[2] = e.summary.deduction
    },
    getSummaries() {
      const sums = []
      sums[0] = '合计'
      sums[11] = this.app.toFix(this.eo_price[0], 2)
      sums[12] = this.app.toFix(this.eo_price[1] - this.eo_price[2], 2)
      sums[13] = this.app.toFix(this.eo_price[0] - (this.eo_price[1] - this.eo_price[2]), 2)
      return sums
    }
  }
}
</script>
